library(redist)
library(tidyverse)
library(tictoc)

# from get_sc_data_original
sc_shp <- read_rds('data/SC/sc_shp.Rds')
sc_adj <- read_rds('data/SC/sc_adj.Rds')

# set up for redist ----
sc_map_orig <- redist_map(sc_shp, existing_plan = cd, pop_tol = 0.01, adj = sc_adj, total_pop = pop)
sc_map_da04 <- redist_map(sc_shp, existing_plan = cd, pop_tol = 0.01, adj = sc_adj, total_pop = v4_pop)
sc_map_da12 <- redist_map(sc_shp, existing_plan = cd, pop_tol = 0.01, adj = sc_adj, total_pop = v12_pop)
sc_map_da19 <- redist_map(sc_shp, existing_plan = cd, pop_tol = 0.01, adj = sc_adj, total_pop = v19_pop)

# Run SMC -------
fs::dir_create("data/SC/sim/CD") # create container
sim_CD <- function(data, .nsims = 2e5) {
# sim_CD <- function(data, .nsims = 2e5) {
    redist_smc(data,
               nsims = .nsims,
               counties = County,
               verbose = TRUE)
}


plans_da19 <- sim_CD(sc_map_da19)
write_rds(plans_da19, "data/SC/sim/CD/plans_da19.rds", compress = "xz")

plans_da12 <- sim_CD(sc_map_da12)
write_rds(plans_da12, "data/SC/sim/CD/plans_da12.rds", compress = "xz")

plans_orig <- sim_CD(sc_map_orig)
write_rds(plans_orig, "data/SC/sim/CD/plans_orig.rds", compress = "xz")

plans_da04 <- sim_CD(sc_map_da04)
write_rds(plans_da04, "data/SC/sim/CD/plans_da04.rds", compress = "xz")


# save map
write_rds(sc_map_orig, "data/SC/sim/CD/redist_map_orig.rds")
